package com.yuwubao.uav.tool.util;

import java.util.concurrent.*;

public class TimeoutUtil {

    public static <T> T execute(Callable<T> task, int seconds) {
        ExecutorService threadPool = Executors.newCachedThreadPool();

        T t = null;
        try {
            Future<T> future = threadPool.submit(task);
            t = future.get(seconds, TimeUnit.SECONDS);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            threadPool.shutdownNow();
        }

        return t;
    }
}
